import cv2 as cv
import numpy as np


def object_detect_demo(image):
    gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
    contours, _= cv.findContours(binary,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)

    for i,contour in enumerate(contours):
        area = cv.contourArea(contour)
        print('area :%s'%area)
        x,y,w,h = cv.boundingRect(contour)
        mm = cv.moments(contour)
        cx,cy = mm['m10']/mm['m00'],mm['m01']/mm['m00']
        cv.circle(image,(np.int(cx),np.int(cy)),3,(0,255,255),-1)
        cv.rectangle(image,(x,y),(x+w,y+h),(0,0,255),1)
    cv.imshow('contours',image)


src = cv.imread('detect_blob.png',1)
cv.namedWindow('demo',cv.WINDOW_AUTOSIZE)
cv.imshow('demo',src)
object_detect_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()